/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dto.LoginDTO;
import dto.RoomTypeDTO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import util.DBConfig;

/**
 *
 * @author aptech
 */
public class RoomTypeDAO {
    DBConfig con;
    Connection connect;
    ArrayList<RoomTypeDTO> list = null;
    String SQL_CREATE = "INSERT INTO RoomTypes VALUES(?,?,?)";
    String SQL_READALL = "SELECT * FROM RoomTypes";
    String SQL_READBYID = "SELECT * FROM RoomTypes WHERE TypeID = ?";
    
    
    String SQL_UPDATE = "UPDATE RoomTypes SET Name=?, Price=?  Description=? WHERE TypeID=?";
    String SQL_DELETE = "DELETE RoomTypes WHERE TypeID=?";

    public RoomTypeDAO() {
        con = new DBConfig();
        list = new ArrayList<RoomTypeDTO>();
    }

    public RoomTypeDTO create(RoomTypeDTO dto) {
        try {
            connect = con.OpenConnect();
            PreparedStatement pst = connect.prepareStatement(SQL_CREATE);
            pst.setString(1, dto.getTypename());
            pst.setDouble(2, dto.getPrice());
            pst.setString(3, dto.getDescription());
            pst.execute();
            return dto;
           
        } catch (SQLException ex) {
            Logger.getLogger(LoginDAO.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally {
            con.CloseConnect(connect);
        }
    }

    public ArrayList<RoomTypeDTO> realAll() {
        try {
            connect = con.OpenConnect();
            Statement st = connect.createStatement();
            ResultSet rs = st.executeQuery(SQL_READALL);
            if (rs != null) {
                while (rs.next()) {
                    RoomTypeDTO dto = new RoomTypeDTO(rs.getInt(1), rs.getString(2), rs.getDouble(3), rs.getString(4));
                    list.add(dto);
                }
                return list;
            }
            return null;
        } catch (SQLException ex) {
            Logger.getLogger(LoginDAO.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally {
            con.CloseConnect(connect);
        }

    }

    public RoomTypeDTO readByID(int id) {
        try {
            connect = con.OpenConnect();
            PreparedStatement pst = connect.prepareStatement(SQL_READBYID);
            pst.setInt(1, id);
            ResultSet rs = pst.executeQuery();
            if (rs != null) {
                if (rs.next()) {
                    RoomTypeDTO dto = new RoomTypeDTO(rs.getInt(1), rs.getString(2), rs.getDouble(3), rs.getString(4));
                    return dto;
                }
            }
            return null;
        } catch (SQLException ex) {
            Logger.getLogger(LoginDAO.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally {
            con.CloseConnect(connect);
        }
    }
    
    

    public boolean update(LoginDTO login) {
        return false;
    }

    public boolean delete(int id) {
        return false;
    }

    public static void main(String[] args) {
        LoginDAO dao = new LoginDAO();
        dao.create(new LoginDTO(1, "Wwewe", "ERWERER"));
    }
}
